package com.song.ssm.month.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author 宋伟宁
 * @Date 2023/9/25
 * @Version 1.0
 **/
public class LogInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //拦截访问控制器的请求：
       //list:列表要求必须登录后才可访问，确保session有需要的值，同时放过登录的请求和登录处理的请
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
        System.out.println("---->"+uri);
        if(session.getAttribute("logUser")!=null || uri.endsWith("login")){
            return true;
        }else{
             response.sendRedirect("login");
        }
        return  false;
    }
}
